Word Ladder

Medium

Question

Given a set of valid words, determine the minimum number of single-character transformations it would take to transform from a starting word to an ending word.

If there is not a possible way to transform frome one word to another, then return 0.

Input: start = "cat", end = "dog", words = set(["cat", "cot", "hot", "dog", "dot"])

Output: 3

It would take a minimum of 3 single-character transformations to transform from "cat" to "dog": "cat" => "cot" => "dot" => "dog".

Input: start = "hot", end = "cog", words = set(["cog", "dot", "fog", "lot", "hot", "job"])

Output: 0

There is no way to transform from "hot" to "cog" one letter at a time based on the words available in word_set.

Input: start = "hot", end = "cog", words = set(["cod", "cog", "cot", "fog", "rod", "rot", "hot", "job"])

Output: 2

Though there are multiple ways to transform from "hot" to "cog", the most minimum steps required is 2 tranformations: "hot" => "cot" => "cog".

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

What is the number of transformations required to transform from "pop" to "map" if the available words are: "bop", "hop", "had", "mad", "map", "mop", "pop"?
0
3
4
5

Login or signup to save your code.

Notes